﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace DotNetExtensions.Core
{
    public class FixedList<T> : IList<T>
    {
        private int currentValueIndex = 0;
        private readonly List<T> Values;
        private readonly int Size;
        public FixedList(int size)
        {
            this.Size = size;
            Values = new List<T>(size);
            for (int i = 0; i < size; i++)
            {
                Values.Add(default(T));
            }
        }
        public T this[int index] { get => Values[index]; set => Values[index] = value; }

        public int Count => Size;

        public bool IsReadOnly => false;

        public void Add(T item)
        {
            lock (this)
            {
                if (currentValueIndex >= Size)
                {
                    for (int i = 0; i < Size - 1; i++)
                    {
                        Values[i] = Values[i + 1];
                    }
                    Values[Size - 1] = item;
                }
                else
                {
                    Values[currentValueIndex] = item;
                    currentValueIndex += 1;
                }
            }
        }

        public void Clear()
        {
            lock (this)
            {
                for (int i = 0; i < Size; i++) Values[i] = default(T);
            }
        }

        public bool Contains(T item)
        {
            throw new NotImplementedException();
        }

        public void CopyTo(T[] array, int arrayIndex)
        {
            throw new NotImplementedException();
        }

        public IEnumerator<T> GetEnumerator()
        {
            return Values.GetEnumerator();
        }

        public int IndexOf(T item)
        {
            throw new NotImplementedException();
        }

        public void Insert(int index, T item)
        {
            lock (this)
            {
                if (index == 0)
                {
                    Values[index] = item;
                }
                else if (index > 0 && index < Size - 1)
                {
                    for (int i = 0; i < index; i++)
                    {
                        Values[i] = Values[i + 1];
                    }
                    Values[Size - 1] = item;
                }
            }
        }

        public bool Remove(T item)
        {
            throw new NotImplementedException();
        }

        public void RemoveAt(int index)
        {
            throw new NotImplementedException();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return Values.GetEnumerator();
        }
    }
}
